package com.example.zzh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zzh.entity.ZzhProductAttribute;
import com.example.zzh.entity.ZzhProductUnit;
import com.example.zzh.entity.ZzhRole;
import com.example.zzh.service.ZzhProductAttributeService;
import com.example.zzh.service.ZzhRoleService;
import com.example.zzh.utils.Result;
import com.example.zzh.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 产品属性表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/zzh-product-attribute")
public class ZzhProductAttributeController {
    @Autowired
    private ZzhProductAttributeService zzhProductAttributeService;


    @GetMapping("/list")
    @ApiOperation(value = "查询所有属性(查询)", notes = "查询所有属性信息")
    public Result list(){
        return ResultGenerator.genSuccessResult(zzhProductAttributeService.list());
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询所有属性分页", notes = "分页")
    public Result list(@RequestBody Map<String,Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        Page<ZzhProductAttribute> page=new Page<>();
        page.setCurrent((int) params.get("page"));
        page.setSize((int) params.get("limit"));
        LambdaQueryWrapper<ZzhProductAttribute> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage r=null;
        if(!StringUtils.isEmpty(params.get("name"))){
            lambdaQueryWrapper.like(ZzhProductAttribute::getAttributeName,params.get("name"));
        }
        lambdaQueryWrapper.in(ZzhProductAttribute::getStatus,1,2);
        r=zzhProductAttributeService.pageNames(page,lambdaQueryWrapper);
        return ResultGenerator.genSuccessResult(r.getRecords(),r.getTotal());
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增属性", notes = "新增属性信息")
    public Result add(@RequestBody ZzhProductAttribute zzhProductAttribute){
        if(zzhProductAttributeService.save(zzhProductAttribute)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改属性", notes = "修改属性信息")
    public Result mod(@RequestBody ZzhProductAttribute zzhProductAttribute){
        if(zzhProductAttributeService.updateById(zzhProductAttribute)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"修改不成功");
        }
    }
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除属性", notes = "删除属性信息")
    public Result delete(@RequestParam Long id){
        ZzhProductAttribute zzhProductAttribute=zzhProductAttributeService.getRoleById(id);
        if(!Objects.isNull(zzhProductAttribute)){
            zzhProductAttribute.setStatus(0);
            if(zzhProductAttributeService.updateById(zzhProductAttribute)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "修改计量单位状态", notes = "修改计量单位状态")
    public Result updateStatus(@RequestParam Long id){
        ZzhProductAttribute zzhProductAttribute=zzhProductAttributeService.getRoleById(id);
        if(!Objects.isNull(zzhProductAttribute)){
            if(zzhProductAttribute.getStatus()==1){
                zzhProductAttribute.setStatus(2);
            }else if(zzhProductAttribute.getStatus()==2){
                zzhProductAttribute.setStatus(1);
            }
            if(zzhProductAttributeService.updateById(zzhProductAttribute)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"更新不成功");
        }
    }


    @PostMapping("/findeByName")
    @ApiOperation(value = "查询是否重复", notes = "查询是否重复")
    public Result findeByName(@RequestParam String name){
        List list=zzhProductAttributeService.lambdaQuery().eq(ZzhProductAttribute::getAttributeName,name).list();
        return list.size()>0?ResultGenerator.genSuccessResult(list):ResultGenerator.genFailResult("不重复");
    }
}

